package com.cy.dda.service.rule.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cy.dda.comm.constant.Constants;
import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.dao.mapper.rule.ManagerMapper;
import com.cy.dda.model.PageBean;
import com.cy.dda.model.Result;
import com.cy.dda.model.domain.rule.Manager;
import com.cy.dda.model.dto.rule.ManagerLoginDTO;
import com.cy.dda.model.enums.CommonStatus;
import com.cy.dda.model.query.rule.ManagerListAdminQuery;
import com.cy.dda.model.vo.rule.ManagerListAdminVO;
import com.cy.dda.service.BaseService;
import com.cy.dda.service.rule.ManagerService;

/**
 * 后台管理员 service
 *
 * @author: tanyilin
 * @create: 2018年10月31日 下午6:04:17
 */
@Service
public class ManagerServiceImpl extends BaseService implements ManagerService {
	
	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private ManagerMapper managerMapper;

	@Override
	public Result login(String mobile, String password) {
		password = CommonUtils.md5Passwod(password);
		ManagerLoginDTO managerLoginDTO = managerMapper.login(mobile, password);
		state(managerLoginDTO != null, "手机号或密码错误");
		
		CommonStatus status = managerLoginDTO.getStatus();
		
		//判断管理员账户状态
        switch (status) {
	        case DISABLE:
	            return Result.fail("该账户已被禁用，请联系超级管理员");
	
	        case DELETE:
	        	return Result.fail("不存在该账户");
	        	
	        default:
	            break;
	    }
		
        //生成token
        String token = UUID.randomUUID().toString();
        managerLoginDTO.setToken(token);
        //生成key
        String key = Constants.ADMIN_LOGIN_KEY.concat(token);
        //将管理员用户信息放到redis缓存
        redisTemplate.opsForValue().set(key, managerLoginDTO, Constants.LOGIN_TIME, Constants.LOGIN_TIME_UNIT);
        
		return Result.success("登录成功", managerLoginDTO);
	}

	@Override
	@Transactional
	public void addManger(Manager manager) {
		
		//验证手机号码是否已存在
    	Manager dbManager = new Manager();
    	dbManager.setMobile(manager.getMobile());
    	dbManager = managerMapper.selectOne(dbManager);
    	state(dbManager == null, "该用户手机号码已存在");
		
    	Date time = new Date();
    	manager.setPassword(CommonUtils.md5Passwod(Constants.INIT_PASSWORD));
    	manager.setStatus(CommonStatus.ENABLE);
    	manager.setCreateTime(time);
    	manager.setUpdateTime(time);
    	managerMapper.insertSelective(manager);
    	
	}

	@Override
	@Transactional
	public void updateManager(Manager manager) {

		manager.setUpdateTime(new Date());
        managerMapper.updateByPrimaryKeySelective(manager);
	}

	@Override
	@Transactional
	public void updateStatus(Long managerId, CommonStatus status) {
		
		Manager manager = new Manager();
		manager.setId(managerId);
		manager.setStatus(status);
		manager.setUpdateTime(new Date());
		managerMapper.updateByPrimaryKeySelective(manager);
	}

	@Override
	@Transactional
	public void updatePassword(Long managerId, String oldPassword, String newPassword) {

		String md5OldPassword = CommonUtils.md5Passwod(oldPassword);
        String md5NewPassword = CommonUtils.md5Passwod(newPassword);
        
		// 验证旧密码
        Manager dbManager = new Manager();
		dbManager.setId(managerId);
		dbManager.setPassword(md5OldPassword);
		dbManager = managerMapper.selectOne(dbManager);
        state(dbManager != null, "旧密码不正确");
        
        // 修改新密码
        Manager manager = new Manager();
        manager.setId(managerId);
        manager.setPassword(md5NewPassword);
        manager.setUpdateTime(new Date());
        managerMapper.updateByPrimaryKeySelective(manager);
	}

	@Override
	@Transactional
	public void resetManagerPwd(Long managerId) {

		Manager manager = new Manager();
		manager.setId(managerId);
		manager.setPassword(CommonUtils.md5Passwod(Constants.INIT_PASSWORD));
		manager.setUpdateTime(new Date());
		managerMapper.updateByPrimaryKeySelective(manager);
	}

	@Override
	public PageBean<ManagerListAdminVO> selectManagerListAdmin(ManagerListAdminQuery query) {
		int total = managerMapper.selectManagerListAdminCount(query);
		List<ManagerListAdminVO> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = managerMapper.selectManagerListAdmin(query);
		}
		return new PageBean<ManagerListAdminVO>(list, total);
	}

}
